package myJs.components

import com.softwaremill.quicklens._
import japgolly.scalajs.react.ReactMonocle._
import japgolly.scalajs.react._
import japgolly.scalajs.react.component.Scala.BackendScope
import japgolly.scalajs.react.extra.StateSnapshot
import japgolly.scalajs.react.vdom.all._
import monocle.Lens
import monocle.macros._
import myJs.Implicits._

import scala.collection.immutable.SeqMap
import scala.scalajs.js

/**
 * Created by yz on 3/11/2020
 */
object SummaryComponent {

  @Lenses
  case class State(summaryDict: JDJAn)

  object State {

    def init = {
      State(
        summaryDict = js.Dictionary[js.Any]()
      )
    }
  }

  class Backend(bs: BackendScope[Unit, State]) {

    val fieldNames = List()

    def didMount(s: State): Callback = {
      Callback {

      }
    }

    def didUpdate(preS: State, curS: State): Callback = {
      Callback {

      }
    }

    def unmount: Callback = {
      Callback {

      }
    }

    def render(s: State) = {

      implicit class MyLens[T](lens: Lens[State, T]) {

        def zoomL(implicit r: Reusability[T]) = {
          StateSnapshot.withReuse.zoomL(lens).prepareVia(bs).apply(s)
        }

      }

      val summaryDict = s.summaryDict
      div(

        summaryDict.toList.filter { case (k, v) =>
          k != "Genomic View"
        }.map { case (k, v) =>
          val trueV = k match {
            case "Variation Type" => val vStr = v.toString
              val vs = vStr.split("\\s+")
              span(s"${vs.head} ",
                span(`class` := "small-font gray", s"${vs.tail.mkString(" ")}")
              )
            case "Frequency" => val vs = v.toJArS
              div(maxHeight := "90px", overflowY := "auto", width := "100%",
                vs.map { v =>
                  span(v)
                }.mkReactFragment(br)
              )
            case _ => span(v.toString)
          }
          div(
            label(`class` := "control-label col-md-2", s"${k}:"),
            div(`class` := "col-sm-4",
              p(`class` := "form-control-static", width := "100%", trueV)
            )
          )
        }.grouped(2).map { es =>
          div(`class` := "row",
            div(`class` := "form-group",
              es.toVdomArray
            )
          )
        }.toVdomArray,
      )
    }

  }

  def component(state: State) = {
    ScalaComponent.builder[Unit].initialState(state).renderBackend[Backend].
      componentDidMount(x => x.backend.didMount(x.state)).componentWillUnmount(_.backend.unmount).
      componentDidUpdate(x => x.backend.didUpdate(x.prevState, x.currentState)).
      build
  }

  val Component = component(State.init)

}



